<template>
  <div style="width:1300px;">
    <el-row :gutter="20"
            justify="center"
            type="flex">
      <el-col :span="2">
        <router-link to="/">
          <img class="logo"
               :src="icon">
        </router-link>
      </el-col>
      <el-col :span="18"
              class="flex-mene-list">
        <el-menu :default-active="active"
                 class="el-menu-demo"
                 mode="horizontal"
                 active-text-color="#409eff"
                 @select="handleSelect">
          <el-menu-item v-for="(item,index) in menu"
                        :key="index"
                        :index="item.key">{{item.value}}</el-menu-item>
        </el-menu>
        <span class="menu-search">
          <i class="el-icon-search"
             @click="clickSearch"></i>
        </span>
      </el-col>
      <el-col :span="4"
              class="flex-header-right">
        <template v-if="logined">
          <el-avatar class="j-user-avatar"
                     :src="avatar"></el-avatar>
          <el-button class="login-out-btn"
                     size="small"
                     type="text"
                     @click="quit">退出</el-button>
        </template>
        <template v-else>
          <router-link :to="{name: 'login'}"
                       class="router-link-customize">
            登录
          </router-link>
          <router-link :to="{name: 'register'}"
                       class="router-link-customize">
            注册
          </router-link>
        </template>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { mapActions, mapGetters } from 'vuex'

export default {
  name: 'menu-temp',
  inject: ['reload'],
  data () {
    return {
      menu: [
        {
          key: "/",
          value: "首页",
        },
        {
          key: "/topics",
          value: "归档"
        },
        {
          key: "/archive",
          value: "时间轴"
        },
        {
          key: "/project",
          value: "项目"
        },
        {
          key: "/about",
          value: "关于我"
        },
        {
          key: "/message",
          value: "留言"
        },
        {
          key: "/github",
          value: "Github"
        },
      ],
    }
  },
  computed: {
    ...mapGetters('login', ['logined', 'avatar']),
    ...mapGetters('home', ['logo']),
    active () {
      return this.$route.path
    },
    icon () {
      const { url } = this.logo;
      return url || '../../../public/logo.png';
    }
  },
  methods: {
    ...mapActions('login', ['loginOut']),
    handleSelect (key, keyPath) {
      if (this.$router.currentRoute.path === key) {
        return
      }
      this.$router.push({ path: key })
    },
    clickSearch () {
      this.$searchOpen()
    },
    quit () {
      this.loginOut()
      const { path } = this.$router
      this.reload()
    }
  }
}
</script>
<style scoped lang="scss">
.logo {
  height: 50px;
  margin: 0;
  border-radius: 50%;
  margin-top: 5px;
}
.el-menu.el-menu--horizontal {
  border-bottom: none;
}
.el-menu--horizontal > .el-menu-item {
  cursor: pointer;
  color: #333;
}
.flex-mene-list {
  position: relative;
  .el-menu-demo {
    // z-index: 99;
  }
  .menu-search {
    display: inline-flex;
    position: absolute;
    top: 0;
    right: 0;
    height: 100%;
    align-items: center;
    margin-right: 10px;
    i {
      cursor: pointer;
      padding: 10px 15px;
    }
  }
}
.flex-header-right {
  display: flex;
  align-items: center;
}
.j-user-avatar {
  cursor: pointer;
}
.login-out-btn {
  margin-left: 10px;
}
</style>
